package com.example.basic.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ClassName: InterceptorConfig
 * Description:
 * date: 2021/12/14 11:59
 * author zesion lee
 */
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    @Resource
    JwtInterceptor jwtInterceptor;


    @Override
    protected void addInterceptors(InterceptorRegistry registry) {

        List<String> excludePaths = Arrays.asList("/doc.html", "/webjars/**", "/swagger-resources/**", "/api/**", "/error","/user/login");
        registry.addInterceptor(jwtInterceptor).excludePathPatterns(excludePaths);
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("POST", "GET", "PUT", "DELETE")
                .allowedOriginPatterns("*")
                .allowedHeaders("*")
                .exposedHeaders("*");
    }
}
